<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
jimport('joomla.filesystem.file');
class QuangcaoController extends JController
{
	function __construct($config = array())
	{
		parent::__construct($config);
		$this->registerTask( 'add',  'display' );
		$this->registerTask( 'edit', 'display' );
		$this->registerTask( 'publish', 'published' );
		$this->registerTask( 'unpublish', 'unpublished' );
	}
	function display( )
	{
		switch($this->getTask())
		{
			case 'add'     :
			{
				JRequest::setVar( 'hidemainmenu', 0 );
				JRequest::setVar( 'layout', 'default'  );
				JRequest::setVar( 'view'  , 'detail');
				JRequest::setVar( 'edit', false );
			} break;
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 0 );
				JRequest::setVar( 'layout', 'default'  );
				JRequest::setVar( 'view'  , 'detail');
				JRequest::setVar( 'edit', true );
			} break;
			default:
			{
				JRequest::setVar('view','list');
				JRequest::setVar('layout','default');
			}
		}
		parent::display();
	}
	function uploads($file){
		$temp =  substr(md5(date('l jS \of F Y h:i:s A').rand(0,100)),0,15).'_';
		$temp.= strtolower(str_replace(' ','_',$file['name']));
		$destImage=JPATH_SITE.DS.'uploads'.DS.'image'.DS.$temp;
		JFile::upload($file['tmp_name'], $destImage);	
		return $temp;
	}
	function save()
	{		
		$post	= JRequest::get('post');
		$date = JFactory::getDate();
		if($post['createday']=='')
			$post['createday'] = $date->toMySQL();
		$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$post['id'] = (int) $cid[0];
		if($cid[0]){
            $db = & JFactory::getDBO();
            $query = 'select image from #__quangcao where id='.$cid[0];
            $db->setQuery($query);
            $image = $db->loadResult();
			
        }
		////////////////////////////////////////////////
		$oldimage = $post['oldimage'];
		$temp =  substr(md5(date('l jS \of F Y h:i:s A').rand(0,100)),0,15).'_';
		$file = JRequest::getVar('image', null, 'files', 'array');
		if($file){
		$filename = JFile::makeSafe($temp.$file['name']); 
		$src = $file['tmp_name'];
		$dest = JPATH_SITE . DS . "uploads" . DS . "image" . DS . $filename;
		switch(strtolower(JFile::getExt($filename) ))
		{
			case 'jpg':
			case 'png':
			case 'gif':
			case 'bmp':
			case 'jpeg':	
			case 'pdf':		
			{
				if ( JFile::upload($src, $dest) ) {	
				$post['image'] = $filename;	
				$path_oldimage = JPATH_SITE . DS . "uploads" . DS . "image" . DS . $oldimage;			
				if($oldimage)	{					
					JFile::delete($path_oldimage);					
								}
		   											}
			}
		}		
		}	
		////////////////////////////////////////////////
		$model = $this->getModel('detail');
		if ($model->store($post)) {
			$msg = JText::_( 'Lưu thành công' );
		} else {
			$msg = JText::_( 'Có lỗi' );
		}
		$link = 'index.php?option=com_quangcao';
		$this->setRedirect($link, $msg);
		
	}
	function published()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_quangcao' );
	}
	function unpublished()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_quangcao' );
	}
	function remove()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_quangcao','Đã xóa' );
	}
	function cancel()
	{
		$this->setRedirect( 'index.php?option=com_quangcao' );
	}
	function orderup()
	{
		$model = $this->getModel('detail');
		$model->move(-1);
		$this->setRedirect( 'index.php?option=com_quangcao');
	}
	function orderdown()
	{
		$model = $this->getModel('detail');
		$model->move(1);
		$this->setRedirect( 'index.php?option=com_quangcao');
	}
	function saveorder()
	{
		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'ordering', array(), 'post', 'array' );
		
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);

		$model = $this->getModel('detail');
		$model->saveorder($cid, $order);

		$msg = JText::_( 'New ordering saved' );
		$this->setRedirect( 'index.php?option=com_quangcao', $msg );
	}
}